package com.itheima.controller;

import com.itheima.excption.MyException;
import com.itheima.holder.AdminLocal;
import com.itheima.pojo.UmsRole;
import com.itheima.util.JWTUtil;
import com.itheima.vo.PageResultVo;
import com.itheima.vo.Result;
import com.itheima.pojo.UmsAdmin;
import com.itheima.service.UmsAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin")
public class UmsAdminController {
    @Autowired
    private UmsAdminService service;

    //登出
    @PostMapping("/logout")
    public Result logout(HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.invalidate();
        return Result.success(null);
    }

    //登录
    @PostMapping("/login")
    public Result login(@RequestBody UmsAdmin umsAdmin) {
        return service.login(umsAdmin);
    }

    //获取登录用户信息
    //todo;id要改
    @GetMapping("/info")
    public Result info() {
        Long userId = AdminLocal.getUserId();
        return service.findById(userId);
    }

    //删除指定用户
    @PostMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Long userId) {
        service.deleteById(userId);
        return Result.success(null);
    }

    //分页查询用户信息
    @GetMapping("/list")
    public Result list(String keyword, @RequestParam(defaultValue = "1") Integer pageNum
            , @RequestParam(defaultValue = "5") Integer pageSize) {
        PageResultVo resultVo = service.list(keyword, pageNum, pageSize);
        return Result.success(resultVo);
    }

    //用户注册
    @PostMapping("/register")
    public Result register(@RequestBody UmsAdmin admin) {
        service.register(admin);
        return Result.success(null);
    }

    //刷新token
    @GetMapping("/refreshToken")
    public Result refreshToken(HttpServletRequest request) {
        String header = request.getHeader("Authorization");
        String token = header.replace("Bearer ", "");
        boolean flag = JWTUtil.verifyToken(token);
        if (flag) return Result.success(null);
        else return Result.error(null, "登录状态已失效,请重新登录");
    }

    //给用户分配角色
    @PostMapping("/role/update")
    public Result updateRole(@RequestParam Integer adminId,
                             @RequestParam List<Integer> roleIds) {
        service.updateRole(adminId, roleIds);
        return Result.success(null);
    }
    //获取指定用户的角色信息
    @GetMapping("/role/{adminId}")
    public ResponseEntity getRoles(@PathVariable("adminId") Long adminId){
       List<UmsRole>roles= service.getRoles(adminId);
       return ResponseEntity.ok(Result.success(roles));
    }
    //修改指定用户信息
    @PostMapping("/update/{id}")
    public ResponseEntity update(@RequestBody UmsAdmin admin,@PathVariable("id")Long id){
        admin.setId(id);
        service.updateAdmin(admin);
        return ResponseEntity.ok(Result.success(null));
    }
    //修改用户密码
    @PostMapping("/updatePassword")
    public ResponseEntity updatePassword(@RequestBody Map map){
        String newPassword = (String) map.get("newPassword");
        String oldPassword = (String) map.get("oldPassword");
        String username = (String) map.get("username");
        service.updatePassword(newPassword,oldPassword,username);
        return ResponseEntity.ok(Result.success(null));
    }
    //修改状态
    @PostMapping("/updateStatus/{id}")
    public ResponseEntity updateStatus(@PathVariable("id")Long id, Integer status){
        if (id==3)throw new MyException("不能修改超级管理员的状态");
       service.updateStatus(id,status);
       return ResponseEntity.ok(Result.success(null));
    }
    //查询指定id用户信息
    @GetMapping("/{id}")
    public ResponseEntity getById(@PathVariable("id")Long id){
       UmsAdmin admin= service.find(id);
        return ResponseEntity.ok(Result.success(admin));
    }
}
